import pickle


class Demo(object):

    def __new__(cls, *args, **kwargs):
        print("__new__() 初始化时被调用")
        print('这个是*args', *args)
        print('kwargs', **kwargs)
        return object.__new__(cls)

    def __init__(self, name, age, birthday):
        print("__init__()  初始化时被调用")
        self.name = name
        self.age = age
        self.birthday = birthday
        print(self.name, self.age, self.birthday)

    def __del__(self):
        class_name = self.__class__.__name__
        print(class_name, "销毁对象时调用")

    # toString
    def __str__(self):
        msg = "--> {name},{age},{birthday}".format(name=self.name, age=self.age, birthday=self.birthday)
        return msg

    # 比较操作
    def __cmp__(self, other):
        return self.age == other.age

    def __gt__(self, other):
        return self.age > other.age

    def __lt__(self, other):
        return self.age < other.age

    # 序列化
    def __getstate__(self):
        return {
            'name': self.name,
            'age': self.age,
            'birthday': self.birthday
        }

    def __setstate__(self, state):
        self.name = state['name']
        self.age = state['age']
        self.birthday = state['birthday']

    def __call__(self, name, age):
        self.name = name
        self.age = age


demo = Demo('王莎丽', 20, (2000, 2, 23))
print(demo)

p = pickle.dumps(demo)
p2 = pickle.loads(p)
print(p2.name, p2.age, p2.birthday)


print("-----------------------------------")
# 内置控制访问属性
print(Demo.__doc__)
print(Demo.__name__)
print(Demo.__module__)
print(Demo.__bases__)
print(Demo.__dict__)
del demo


class g(float):
    # kg->斤
    def __new__(cls, kg):
        return float.__new__(cls, kg * 2)


a = g(50)
print(a)
print(a + 100)
